/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.managedbeans;

import com.blog.city.checkthedep.entities.Article;
import com.blog.city.checkthedep.sessionbeans.ArticleFacadeLocal;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedProperty;

/**
 *
 * @author Breath
 */
@ManagedBean
@RequestScoped
public class ArticleBean {

    private List<Article> articles;
    private List<Article> ownArticles;
    private Article latestArticle;
    private Article selectedArticle = new Article();
    @EJB
    private ArticleFacadeLocal articleFac;

 
    /**
     * 
     */
    @PostConstruct
    public void retrieveArticles() {
        this.setArticles(articleFac.findAll());
    }
    
    @ManagedProperty(value = "#{loginBean}")
    private LoginBean loginBean;

    /**
     * 
     * @return 
     */
    public LoginBean getLoginBean() {
        return loginBean;
    }

    /**
     * 
     * @param loginBean
     */
    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }

    /**
     * 
     */
    public void setAuthor() {
        selectedArticle.setRegisteredUserId(loginBean.getCheckedUser());
    }

    /**
     * 
     * @return
     */
    public Article getSelectedArticle() {
        return selectedArticle;
    }

    /**
     * 
     * @param selectedArticle
     */
    public void setSelectedArticle(Article selectedArticle) {
        this.selectedArticle = selectedArticle;
    }

    /**
     * 
     * @return
     */
    public ArticleFacadeLocal getArticleFac() {
        return articleFac;
    }

    /**
     * 
     * @param articleFac
     */
    public void setArticleFac(ArticleFacadeLocal articleFac) {
        this.articleFac = articleFac;
    }

    /**
     * 
     * @return
     */
    public List<Article> getArticles() {
        return articles;
    }

    /**
     * 
     * @param articles
     */
    public void setArticles(List<Article> articles) {
        this.articles = articles;
    }

    /**
     * 
     * @return
     */
    public List<Article> getOwnArticles() {
        return ownArticles;
    }

    /**
     * 
     * @param ownArticles
     */
    public void setOwnArticles(List<Article> ownArticles) {
        this.ownArticles = ownArticles;
    }

    /**
     * 
     * @return
     */
    public List<Article> retrieveOwnArticles() {
        ownArticles = articleFac.findByRegUserId(loginBean.getCheckedUser());
        return ownArticles;
    }

    /**
     * 
     * @return
     */
    public Article getLatestArticle() {
        return latestArticle;
    }

    /**
     * 
     * @param latestArticle
     */
    public void setLatestArticle(Article latestArticle) {
        this.latestArticle = latestArticle;
    }

    /**
     * 
     * @return
     */
    public Article retrieveLatest() {
        latestArticle = articleFac.latestArt();
        return latestArticle;
    }

    /**
     * 
     * @return
     */
    public String persistArticle() {
        if (selectedArticle != null) {
            setAuthor();
            articleFac.create(selectedArticle);
        }
        return "showRegMenu";
    }

    /**
     * 
     * @param id
     * @return
     */
    public String retrieveSelectedArticle(Integer id) {
        this.selectedArticle = articleFac.find(id);
        return "showArt";
    }

    /**
     * 
     * @return
     */
    public String persistSelectedArticle() {
        if (selectedArticle != null) {
            setAuthor();
            articleFac.edit(selectedArticle);
        }
        return "showViewArt";
    }

    /**
     * 
     * @param id
     */
    public void deleteSelectedArticle(Integer id) {
        this.retrieveSelectedArticle(id);
        if (selectedArticle != null) {
            articleFac.remove(selectedArticle);
        }
        this.retrieveArticles();
    }
}
